/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Modelo;



import VO.TelefonistaVO;
import VO.TurnoVO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.Vector;
import javax.sql.DataSource;
import utiles.SQL.GeneralOperations;
import utiles.SQL.SimpleDataSource;
import utiles.exceptions.DataBaseException;
import utiles.exceptions.InternalErrorException;

/**
 *
 * @author Jorge
 */
public class DaoTelefonista implements DaoTelefonistaInterface{

    public Vector cargarIdTelefonistas() throws DataBaseException, InternalErrorException, SQLException {
        Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        Vector result = null;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL cargarIdTelefonistas()";

            callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

            callabledStatement = connection.prepareCall(queryString1);

            callabledStatement.execute();

            if ((resultSet = callabledStatement.getResultSet()).isBeforeFirst() != false) {
                result = new Vector();

                while (resultSet.next()) {

                    result.add(resultSet.getString("id"));
                }



            }


        } catch (SQLException e) {
            result = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }
    }

    public boolean ingresarTelefonista(TelefonistaVO nueva) throws DataBaseException, InternalErrorException, SQLException {
        Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        boolean result = false;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL ingresarTelefonista(?,?,?,?,?,?)";

            callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

            callabledStatement = connection.prepareCall(queryString1);
            callabledStatement.setString("rutp", nueva.getRut());
            callabledStatement.setString("nom",nueva.getNombres());
            callabledStatement.setString("apell",nueva.getApellidos());
            callabledStatement.setString("dir",nueva.getDireccion());
            callabledStatement.setString("correo",nueva.getEmail());
            callabledStatement.setString("fono",nueva.getTelefono());

            callabledStatement.execute();

            if (callabledStatement.getUpdateCount()>0)
            {
                result = true;

            }


        } catch (SQLException e) {
            result = false;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }

    }

    public TelefonistaVO cargarTelefonista(String id) throws DataBaseException, InternalErrorException, SQLException {
        Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        TelefonistaVO result = null;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL cargarTelefonista(?)";

            callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

            callabledStatement = connection.prepareCall(queryString1);
            callabledStatement.setString("id", id);

            callabledStatement.execute();

            if ((resultSet = callabledStatement.getResultSet()).isBeforeFirst() != false) {
                result = new TelefonistaVO();

                resultSet.next();

                    result.setApellidos(resultSet.getString("apellidos"));
                    result.setDireccion(resultSet.getString("direccion"));
                    result.setEmail(resultSet.getString("email"));
                    result.setNombres(resultSet.getString("nombres"));
                    result.setRut(resultSet.getString("rut"));
                    result.setTelefono(resultSet.getString("telefono"));





            }


        } catch (SQLException e) {
            result = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }
    }

    public boolean editarTelefonista(TelefonistaVO nueva) throws DataBaseException, InternalErrorException, SQLException {
         Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        boolean result = false;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL editarTelefonista(?,?,?,?,?,?)";

            callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

            callabledStatement = connection.prepareCall(queryString1);
            callabledStatement.setString("rutp", nueva.getRut());
            callabledStatement.setString("nom",nueva.getNombres());
            callabledStatement.setString("apell",nueva.getApellidos());
            callabledStatement.setString("dir",nueva.getDireccion());
            callabledStatement.setString("correo",nueva.getEmail());
            callabledStatement.setString("fono",nueva.getTelefono());

            callabledStatement.execute();

            if (callabledStatement.getUpdateCount()>0)
            {
                result = true;

            }


        } catch (SQLException e) {
            result = false;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }
    }

    public boolean eliminarTelefonista(String id) throws DataBaseException, InternalErrorException, SQLException {
         Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        boolean result = false;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL eliminarTelefonista(?)";

            callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

            callabledStatement = connection.prepareCall(queryString1);
            callabledStatement.setString("id",id);


            callabledStatement.execute();

            if (callabledStatement.getUpdateCount()>0)
            {
                result = true;

            }


        } catch (SQLException e) {
            result = false;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }
    }

    public boolean RegistrarTurno(String id, String fecha) throws DataBaseException, InternalErrorException, SQLException {
        Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        boolean result = false;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL registrarTurno(?,?)";

            callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

            callabledStatement = connection.prepareCall(queryString1);
            callabledStatement.setString("id",id);
            callabledStatement.setString("fechap",fecha);


            callabledStatement.execute();

            if (callabledStatement.getUpdateCount()>0)
            {
                result = true;

            }


        } catch (SQLException e) {
            result = false;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }
    }

    public Vector cargarTurnos() throws DataBaseException, InternalErrorException, SQLException {
        Connection connection = null;
        DataSource dataSource = new SimpleDataSource();
        connection = dataSource.getConnection();
        ResultSet resultSet;
        Vector result = null;
        TurnoVO aux;
        CallableStatement callabledStatement = null;

        try {

            String queryString1 = "CALL cargarTurnos()";

            callabledStatement = connection.prepareCall(queryString1, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

            callabledStatement = connection.prepareCall(queryString1);

            callabledStatement.execute();

            if ((resultSet = callabledStatement.getResultSet()).isBeforeFirst() != false) {
                result = new Vector();

                while (resultSet.next()) {
                    aux = new TurnoVO();

                    aux.setFecha(resultSet.getString("fecha"));
                    aux.setNombre(resultSet.getString("nombre"));
                    aux.setRut(resultSet.getString("rut"));
                    result.add(aux);
                }



            }


        } catch (SQLException e) {
            result = null;
            // si hay un error en la conexion al driver de la BD.
            System.out.println("sql EXCEPTION ingresarDatosGenerales--->" + e.getMessage());
            throw new InternalErrorException(e);
        // llaves duplicadas
        } finally {

            GeneralOperations.closeStatement(callabledStatement);
            GeneralOperations.closeConnection(connection);
            return result;
        }
    }

}
